import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 帅小伙
 * @date 2022/1/3
 * @description
 */
public class Consumer{

    private static final String QUEUE_NAME = "hello";
    public void consume() throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        channel.queueDeclare(QUEUE_NAME,false,false,false,null);
        System.out.println(Thread.currentThread().getName()+"  消费者启动等待消费.........");

        String name = Thread.currentThread().getName();
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(name+" "+message);
            // 类似快递编号
            System.out.println(delivery.getEnvelope().getDeliveryTag());
        };

        channel.basicConsume(QUEUE_NAME, true, deliverCallback, (consumerTag)->{
            System.out.println(consumerTag+"消费者取消消费信息");
        });
    }
}
